package services

import (
	"github.com/labstack/gommon/log"
	"law_case_api/dto"
	"law_case_api/models"
)

// CreateCaseSchedule 创建案件进度事项
func (_case CaseService) CreateCaseSchedule(wzjCaseSchedule models.WzjCaseSchedule) (bool, error) {
	if err := db.Create(&wzjCaseSchedule).Error; err != nil {
		log.Error("CreateCaseSchedule--创建案件进度事项失败,失败原因：", err)
		return false, err
	}
	return true, nil
}

// UpdateCaseSchedule 更新案件进度事项
func (_case CaseService) UpdateCaseSchedule(wzjCaseSchedule models.WzjCaseSchedule) (bool, error) {
	if err := db.Where("id = ?", wzjCaseSchedule.ScheduleId).
		Select("case_id,schedule_content,schedule_time,create_at,create_user,create_user_id").
		Updates(&wzjCaseSchedule).Error; err != nil {
		log.Error("UpdateCaseSchedule--更新案件进度事项失败,失败原因：", err)
		return false, err
	}
	return true, nil
}

// GetCaseScheduleListByCondition 根据案件id获取案件进度事项记录
func (_case CaseService) GetCaseScheduleListByCondition(caseSchedulesListReq dto.CaseSchedulesListReq) ([]models.WzjCaseSchedule, int64, error) {
	var wzjCaseLogs []models.WzjCaseSchedule
	dataCount := int64(0)
	if err := db.Where("case_id=?", caseSchedulesListReq.CaseId).
		Count(&dataCount).
		Select("schedule_id,case_id,schedule_content,schedule_time,create_at,create_user,create_user_id").
		Order("schedule_time desc").
		Find(&wzjCaseLogs).Error; err != nil {
		log.Error("GetCaseScheduleListByCondition--查询失败,失败原因：", err)
		return wzjCaseLogs, dataCount, err
	}
	return wzjCaseLogs, dataCount, nil
}
